package com.lagou.homework.Dao;
/**
 * UserDao接口的实现类
 * 操作数据库
 */

import com.lagou.homework.model.User;
import com.lagou.homework.utls.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserDao {
    @Override
    public User userLogin(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1、获取数据库连接
            connection = DbUtil.getConnection();
            //2、执行sql语句
            String sql = "select * from t_user where userName=? and password=?";
            //3、获取sql语句的结果，
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getPassword());
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()){
                User tu = new User(resultSet.getString("userName"),resultSet.getString("password"));
                return tu;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                //4、释放有关资源
                DbUtil.closeResource(connection,preparedStatement);
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }



        return null;
    }
}
